#!/bin/bash

# This script organizes our out-of-order files by year and month

# Change directory to old_stats, and for every file in old_stats...
cd ~/printers/oo_status/old_stats
for file in $( ls )
do
   
   # First, check to see if $file is a directory. If it is, we don't want to process it, so we move on
   if [ -d $file ]
   then
      continue
   fi

   # Store year as variable $year
   year=`echo $file | awk '{split($0,a,"_"); print a[1]}'`

   # If $year exists and is a directory, move the file into the directory. If
   # the directory doesn't exist, create it and move the file

   if [ -d $year ]
   then
      mv $file $year
      cd $year
   else
      mkdir $year
      mv $file $year
      cd $year
   fi

   # Now we need to get the month and convert to its name
   month_num=`echo $file | awk '{split($0,a,"_"); print a[2]}'`

   case "$month_num" in
      01 ) month="January";;
      02 ) month="February";;
      03 ) month="March";;
      04 ) month="April";;
      05 ) month="May";;
      06 ) month="June";;
      07 ) month="July";;
      08 ) month="August";;
      09 ) month="September";;
      10 ) month="October";;
      11 ) month="November";;
      12 ) month="December";;
   esac

   if [ -d $month ]
   then
      mv $file $month
      cd ..
   else
      mkdir $month
      mv $file $month
      cd ..
   fi

done

exit 0
